﻿using System;
using System.Collections.Generic;
using System.Linq;
using Simple.Data;
using Model.Course;

namespace Model.User
{
    public class Student : Person
    {
        protected Student()
        {
        }

        public Student(Person p)
        {
            int role = p.getRole();
            if (role != 2)
                throw new Exception("Preson ID = " + p.getId() +" is not a student!");

            id = p.getId();
            firstName = p.getFirstName();
            lastName = p.getLastName();
            password = p.getPassword();
            email = p.getEmail();
            picturePath = p.getPicturePath();
            role = p.getRole();
        }

        public static new Student get(int id)
        {
            Person p = Person.get(id);

            Student t = null;

            try
            {
                t = new Student(p);
            }
            catch (Exception e)
            {
                t = null;
            }

            return t;
        }

        public bool isSignedToCourse(int courseId)
        {
            var db = Database.Open();

            int r = db.SignedToCourse.GetCount(db.SignedToCourse.studentId == this.getId()
                                               &&
                                               db.SignedToCourse.courseId == courseId);

            return (r > 0);
        }

        public bool isSignedToCourse(Course.Course course)
        {
            return isSignedToCourse(course.getId());
        }

        public void singInToCourse (Course.Course course)        
        {
            Database.Open().SignedToCourse.Insert(studentId: this.getId(), courseId: course.getId());
        }

        public void singOutFromCourse (Course.Course course)        
        {
            Database.Open().SignedToCourse.Delete(studentId: this.getId(), courseId: course.getId());
        }
    }
}